package com.york.subject.domain.handler.subject;

import com.alibaba.fastjson.JSON;
import com.york.subject.common.enums.IsDeletedFlagEnum;
import com.york.subject.common.enums.SubjectInfoTypeEnum;
import com.york.subject.domain.convert.JudgeSubjectConverter;
import com.york.subject.domain.entity.SubjectAnswerBO;
import com.york.subject.domain.entity.SubjectInfoBO;
import com.york.subject.domain.entity.SubjectOptionBO;
import com.york.subject.infra.basic.entity.SubjectJudge;
import com.york.subject.infra.basic.service.SubjectJudgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 判断题题目的策略类
 */
@Slf4j
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    /**
     * 插入判断题，一对一的
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()) {
            log.info("JudgeTypeHandler.add.bo:{}", JSON.toJSONString(subjectInfoBO));
        }
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    /**
     * 查询判断题选项
     */
    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        // 先查到题目
        List<SubjectJudge> resultList = subjectJudgeService.queryByCondition(subjectJudge);
        // 对题目的答案赋值，用转换器转换格式即可
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertEntityListToBOList(resultList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
